package controller

import (
	"common/helper"
	"common/userHelp"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/valyala/fasthttp"
	"member/model"
	"strconv"
)

type RecordController struct{}

func (that *RecordController) Trade(ctx *fasthttp.RequestCtx) {

	flag := ctx.QueryArgs().GetUintOrZero("flag") //账变类型 271 存款 272 取款
	page := ctx.QueryArgs().GetUintOrZero("page")
	pageSize := ctx.QueryArgs().GetUintOrZero("page_size")

	if page < 1 {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	data, err := model.RecordTrade(uid, flag, uint(page), uint(pageSize))
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}

	helper.Print(ctx, true, data)
}

func (that *RecordController) GameRecord(ctx *fasthttp.RequestCtx) {

	gameTy := ctx.QueryArgs().GetUintOrZero("ty") //1	真人 2	捕鱼 3	电子 4	体育 5	棋牌 6	电竞
	flag := ctx.QueryArgs().GetUintOrZero("flag") //1 今天 7 七天 60 六十天
	gt := ctx.QueryArgs().GetUintOrZero("gt")     //0 全部1为赢的
	page := ctx.QueryArgs().GetUintOrZero("page")
	pageSize := ctx.QueryArgs().GetUintOrZero("page_size")
	if page < 1 {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	data, err := model.GameRecord(uid, gameTy, flag, gt, uint(page), uint(pageSize))
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, data)
}

type inviteRecordParam struct {
	Lvl       string `json:"lvl"`  //123一级二级三级
	Flag      string `json:"flag"` //0全部1有充值2没有充值
	StartTime string `json:"start_time"`
	EndTime   string `json:"end_time"`
	Page      uint   `json:"page"`
	PageSize  uint   `json:"page_size"`
}

func (that *RecordController) InviteRecord(ctx *fasthttp.RequestCtx) {

	param := inviteRecordParam{}
	data := ctx.PostBody()
	err := json.Unmarshal(data, &param)
	if err != nil {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	if param.Page < 1 {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, helper.AccessTokenExpires)
		return
	}

	res, err := model.InviteRecord(uid, param.StartTime, param.EndTime, param.Flag, param.Lvl, uint(param.Page), uint(param.PageSize))
	if err != nil {
		helper.Print(ctx, false, err.Error())
		return
	}
	helper.Print(ctx, true, res)
}

type FlowRecordParam struct {
	QueryType int `json:"QueryType"` // 1全部 2 没有开始 3 进行中 4 已完成
}

// 玩家打码记录
func (that RecordController) FlowRecordList(ctx *fasthttp.RequestCtx) {

	uid := model.GetUidFromToken(ctx)
	if uid == "" {
		helper.Print(ctx, false, errors.New(helper.AccessTokenExpires))
		return
	}

	param := FlowRecordParam{}
	data := ctx.PostBody()
	err := json.Unmarshal(data, &param)
	if err != nil {
		helper.Print(ctx, false, helper.ParamErr)
		return
	}

	if param.QueryType == 1 {
		s, err := model.PromoRecordList(uid)
		if err != nil {
			helper.Print(ctx, false, err.Error())
			return
		}

		helper.Print(ctx, true, s)
	} else if param.QueryType == 2 {
		var data []model.PromoInspection
		WageRequireData := userHelp.GetCurWageReuqire(uid)
		if WageRequireData.ID != 0 {
			SqlSelect := fmt.Sprintf("Select ID,CONVERT(AddTime,CHAR) as AddTime,ChangeMoney,NeedWageRequire,CurWageRequire,Status,ChangeType from tbl_member_wage_require "+
				"where uid=%s and Status=0 and Id!=%d order by ID desc", uid, WageRequireData.ID)
			model.GetSlaveDBInstance().Select(&data, SqlSelect)
		}

		helper.Print(ctx, true, data)

	} else if param.QueryType == 3 {
		var data []model.PromoInspection
		WageRequireData := userHelp.GetCurWageReuqire(uid)

		if WageRequireData.ID != 0 && WageRequireData.Status != 1 {
			var dataItem model.PromoInspection
			dataItem.ID = strconv.FormatInt(WageRequireData.ID, 10)
			dataItem.ChangeMoney = WageRequireData.ChangeMoney
			dataItem.NeedWageRequire = WageRequireData.NeedWageRequire
			dataItem.CurWageRequire = WageRequireData.CurWageRequire
			dataItem.Status = WageRequireData.Status
			data = append(data, dataItem)
		}

		helper.Print(ctx, true, data)
	} else if param.QueryType == 4 {
		var data []model.PromoInspection
		SqlSelect := fmt.Sprintf("Select ID,CONVERT(AddTime,CHAR) as AddTime,ChangeMoney,NeedWageRequire,CurWageRequire,Status,ChangeType from tbl_member_wage_require "+
			"where uid=%s and Status=1  order by ID desc", uid)
		model.GetSlaveDBInstance().Select(&data, SqlSelect)

		helper.Print(ctx, true, data)
	}
}
